package com.pocketsweet.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.media.ExifInterface;
import android.media.ThumbnailUtils;
import android.os.Build;
import android.util.Log;

import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.pocketsweet.MLApplication;
import com.pocketsweet.R;

public class PhotoUtil {

    /**
     * 回收垃圾 recycle
     *
     * @throws
     */
    public static void recycle(Bitmap bitmap) {
        // 先判断是否已经回收
        if (bitmap != null && !bitmap.isRecycled()) {
            // 回收并且置为null
            bitmap.recycle();
            bitmap = null;
        }
        System.gc();
    }

    /**
     * 获取网落图片资源
     *
     * @param url
     * @return
     */
    // public static Bitmap getHttpBitmap(String url) {
    // URL myFileURL;
    // Bitmap bitmap = null;
    // try {
    // myFileURL = new URL(url);
    // // 获得连接
    // HttpURLConnection conn = (HttpURLConnection) myFileURL
    // .openConnection();
    // // 设置超时时间为6000毫秒，conn.setConnectionTiem(0);表示没有时间限制
    // conn.setConnectTimeout(6000);
    // // 连接设置获得数据流
    // conn.setDoInput(true);
    // // 不使用缓存
    // conn.setUseCaches(false);
    // // 这句可有可无，没有影响
    // // conn.connect();
    // // 得到数据流
    // InputStream is = (InputStream) conn.getInputStream();
    // // 解析得到图片
    // bitmap = BitmapFactory.decodeStream(is);
    // // 关闭数据流
    // is.close();
    // } catch (Exception e) {
    // e.printStackTrace();
    // }
    //
    // return bitmap;
    //
    // }

    /**
     * 获取指定路径下的图片的指定大小的缩略图 getImageThumbnail
     *
     * @return Bitmap
     * @throws
     */
    public static Bitmap getImageThumbnail(String imagePath, int width,
                                           int height) {
        Bitmap bitmap = null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        // 获取这个图片的宽和高，注意此处的bitmap为null
        bitmap = BitmapFactory.decodeFile(imagePath, options);
        options.inJustDecodeBounds = false; // 设为 false
        // 计算缩放比
        int h = options.outHeight;
        int w = options.outWidth;
        int beWidth = w / width;
        int beHeight = h / height;
        int be = 1;
        if (beWidth < beHeight) {
            be = beWidth;
        } else {
            be = beHeight;
        }
        if (be <= 0) {
            be = 1;
        }
        options.inSampleSize = be;
        // 重新读入图片，读取缩放后的bitmap，注意这次要把options.inJustDecodeBounds 设为 false
        bitmap = BitmapFactory.decodeFile(imagePath, options);
        // 利用ThumbnailUtils来创建缩略图，这里要指定要缩放哪个Bitmap对象
        bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
                ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
        return bitmap;
    }

    public static void saveBitmap(String dirpath, String filename,
                                  Bitmap bitmap, boolean isDelete) {
        File dir = new File(dirpath);
        // Log.e("文件夹",dirpath);
        if (!dir.exists()) {
            // Log.e("文件夹不存在","不");
            dir.mkdirs();
        }
        File f = new File(dirpath, filename);
        if (f.exists()) {
            f.delete();
        }
        if (!f.exists()) {
            try {
                f.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        // Log.e("图片保存", "第一步");
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(f);
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.flush();
            out.close();
            // Log.e("图片保存", "成功");
            // Log.e("图片保存", f.getAbsolutePath());
            // Log.e("图片保存", f.getPath());

        } catch (FileNotFoundException e) {
            // Log.e("图片保存bug", e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            // Log.e("图片保存bug", e.getMessage());
            e.printStackTrace();
        }
    }

    public static File getFilePath(String filePath, String fileName) {
        File file = null;
        makeRootDirectory(filePath);
        try {
            file = new File(filePath + fileName);
            if (!file.exists()) {
                file.createNewFile();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return file;
    }

    public static void makeRootDirectory(String filePath) {
        File file = null;
        try {
            file = new File(filePath);
            if (!file.exists()) {
                file.mkdirs();
            }
        } catch (Exception e) {

        }
    }

    /**
     * 读取图片属性：旋转的角度
     *
     * @param path 图片绝对路径
     * @return degree旋转的角度
     */

    public static int readPictureDegree(String path) {
        int degree = 0;
        try {
            ExifInterface exifInterface = new ExifInterface(path);
            int orientation = exifInterface.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return degree;

    }

    /**
     * 旋转图片一定角度 rotaingImageView
     *
     * @return Bitmap
     * @throws
     */
    public static Bitmap rotaingImageView(int angle, Bitmap bitmap) {
        // 旋转图片 动作
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        // 创建新的图片
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        return resizedBitmap;
    }

    /**
     * 将图片变为圆角
     *
     * @param bitmap 原Bitmap图片
     * @param pixels 图片圆角的弧度(单位:像素(px))
     * @return 带有圆角的图片(Bitmap 类型)
     */
    public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx = pixels;

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return output;
    }

    /**
     * 将图片转化为圆形头像
     *
     * @throws
     * @Title: toRoundBitmap
     */
    public static Bitmap toRoundBitmap(Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        float roundPx;
        float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
        if (width <= height) {
            roundPx = width / 2;

            left = 0;
            top = 0;
            right = width;
            bottom = width;

            height = width;

            dst_left = 0;
            dst_top = 0;
            dst_right = width;
            dst_bottom = width;
        } else {
            roundPx = height / 2;

            float clip = (width - height) / 2;

            left = clip;
            right = width - clip;
            top = 0;
            bottom = height;
            width = height;

            dst_left = 0;
            dst_top = 0;
            dst_right = height;
            dst_bottom = height;
        }

        Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final Paint paint = new Paint();
        final Rect src = new Rect((int) left, (int) top, (int) right,
                (int) bottom);
        final Rect dst = new Rect((int) dst_left, (int) dst_top,
                (int) dst_right, (int) dst_bottom);
        final RectF rectF = new RectF(dst);

        paint.setAntiAlias(true);// 设置画笔无锯齿

        canvas.drawARGB(0, 0, 0, 0); // 填充整个Canvas

        // 以下有两种方法画圆,drawRounRect和drawCircle
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);// 画圆角矩形，第一个参数为图形显示区域，第二个参数和第三个参数分别是水平圆角半径和垂直圆角半径。
        // canvas.drawCircle(roundPx, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));// 设置两张图片相交时的模式,参考http://trylovecatch.iteye.com/blog/1189452
        canvas.drawBitmap(bitmap, src, dst, paint); // 以Mode.SRC_IN模式合并bitmap和已经draw了的Circle

        return output;
    }

    public static String simpleCompressImage(String path, String newPath) {
        Bitmap bitmap = BitmapFactory.decodeFile(path);
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(newPath);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 80, outputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        recycle(bitmap);
        return newPath;
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
    @SuppressLint("NewApi")
    public static String compressImage(String path, String newPath) {
        int cropSize = 50;// KB
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);
        int inSampleSize = 1;
        int maxSize = 3000;
        Logger.d("outWidth=" + options.outWidth + " outHeight="
                + options.outHeight);
        if (options.outWidth > maxSize || options.outHeight > maxSize) {
            int widthScale = (int) Math.ceil(options.outWidth * 1.0 / maxSize);
            int heightScale = (int) Math
                    .ceil(options.outHeight * 1.0 / maxSize);
            inSampleSize = Math.max(widthScale, heightScale);
        }
        Logger.d("inSampleSize=" + inSampleSize);
        options.inJustDecodeBounds = false;
        options.inSampleSize = inSampleSize;
        Bitmap bitmap = BitmapFactory.decodeFile(path, options);
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        int newW = w;
        int newH = h;
        if (w > maxSize || h > maxSize) {
            if (w > h) {
                newW = maxSize;
                newH = (int) (newW * h * 1.0 / w);
            } else {
                newH = maxSize;
                newW = (int) (newH * w * 1.0 / h);
            }
        }
        Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, newW, newH, false);
        // recycle(bitmap);
        Logger.d("bitmap width=" + newBitmap.getWidth() + " h="
                + newBitmap.getHeight());

        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(newPath);
            // newBitmap = compressImage(newBitmap);
            int photoSize = 0;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                photoSize = newBitmap.getAllocationByteCount();
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {// API
                // 12
                photoSize = newBitmap.getByteCount();
            } else {
                photoSize = bitmap.getRowBytes() * bitmap.getHeight();
            }
            if (photoSize >= cropSize * 1024) {
                int p = cropSize * 1024 * 1024 / photoSize;
//				ToolKits.toast(MLApplication.getContext(),String.valueOf(p));
                if (p > 100) {
                    p = 100;
                }
                newBitmap.compress(Bitmap.CompressFormat.JPEG, p, outputStream);
                // Log.e("==size", "size:" + String.valueOf(photoSize));
                // Log.e("==size", "size:" + String.valueOf(p));
            } else {
                newBitmap.compress(Bitmap.CompressFormat.JPEG, 100,
                        outputStream);

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        recycle(newBitmap);
        recycle(bitmap);
        return newPath;
    }

    private static Bitmap compressImage(Bitmap image) {
        int size = 30;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        // 质量压缩方法，这里100表示不压缩，把压缩后的数据存放到baos中
        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        int options = 100;
        // 循环判断如果压缩后图片是否大于100kb,大于继续压缩
        while (baos.toByteArray().length / 1024 > size) {
//			Log.e("====", "yaaaaaaaaa");
            // 重置baos即清空baos
            baos.reset();
            // 每次都减少10
            options -= 20;
            // 这里压缩options%，把压缩后的数据存放到baos中
            image.compress(Bitmap.CompressFormat.JPEG, options, baos);

        }
        // 把压缩后的数据baos存放到ByteArrayInputStream中
        ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
        // 把ByteArrayInputStream数据生成图片
        Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
        return bitmap;
    }

    public static DisplayImageOptions avatarImageOptions = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.drawable.img_avatar_girl)
            .showImageForEmptyUri(R.drawable.img_avatar_girl)
            .showImageOnFail(R.drawable.img_avatar_girl)
                    // .cacheInMemory(true)
                    // .cacheOnDisc(true)
            .considerExifParams(true)
            .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
            .bitmapConfig(Config.RGB_565)
            .resetViewBeforeLoading(true)
                    // 设置图片在下载前是否重置，复位
            .displayer(
                    new RoundedBitmapDisplayer(ToolKits.dip2px(
                            MLApplication.getContext(), 120)))
                    // .displayer(new FadeInBitmapDisplayer(50))// 淡入
            .build();

    public static DisplayImageOptions squareImageOptions = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.drawable.default_user_avatar)
            .showImageForEmptyUri(R.drawable.default_user_avatar)
            .showImageOnFail(R.drawable.default_user_avatar)
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .considerExifParams(true)
            .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
            .bitmapConfig(Config.RGB_565)
            .resetViewBeforeLoading(true)
                    // 设置图片在下载前是否重置，复位
            .displayer(
                    new RoundedBitmapDisplayer(ToolKits.dip2px(
                            MLApplication.getContext(), 5)))

            .build();
    public static DisplayImageOptions squareImageOptions1 = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.drawable.default_user_avatar)
            .showImageForEmptyUri(R.drawable.default_user_avatar)
            .showImageOnFail(R.drawable.default_user_avatar)
            .considerExifParams(true)
            .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
            .bitmapConfig(Config.RGB_565)
            .resetViewBeforeLoading(true)
                    // 设置图片在下载前是否重置，复位
            .displayer(
                    new RoundedBitmapDisplayer(ToolKits.dip2px(
                            MLApplication.getContext(), 5)))

            .build();

    public static DisplayImageOptions squareRecommendImageOptions = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.drawable.article_bg)
            .showImageForEmptyUri(R.drawable.article_bg)
            .showImageOnFail(R.drawable.article_bg)
            .cacheInMemory(true)
            .cacheOnDisk(true)
                    // .considerExifParams(true)
            .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
            .bitmapConfig(Config.RGB_565)
            .resetViewBeforeLoading(true)

                    // 设置图片在下载前是否重置，复位
            .displayer(
                    new RoundedBitmapDisplayer(ToolKits.dip2px(
                            MLApplication.getContext(), 5))).build();

    public static DisplayImageOptions squareRoundImageOptions = new DisplayImageOptions.Builder()
            // .showImageOnLoading(R.drawable.default_user_avatar)
            // .showImageForEmptyUri(R.drawable.default_user_avatar)
            // .showImageOnFail(R.drawable.default_user_avatar)
            .cacheInMemory(true).cacheOnDisk(true).considerExifParams(true)
            .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
            .bitmapConfig(Config.RGB_565).resetViewBeforeLoading(true).build();
    // 设置图片在下载前是否重置，复位
    // .displayer(
    // new RoundedBitmapDisplayer(ToolKits.dip2px(
    // MLApplication.getContext(), 20))).build();

    public static DisplayImageOptions normalImageOptions = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.drawable.empty_photo)
            .showImageForEmptyUri(R.drawable.empty_photo)
                    // .showImageOnFail(R.drawable.image_load_fail)
            .cacheInMemory(true).cacheOnDisk(true).considerExifParams(true)
            .imageScaleType(ImageScaleType.EXACTLY)
            .bitmapConfig(Config.RGB_565).resetViewBeforeLoading(true)// 设置图片在下载前是否重置，复位
                    // .displayer(new RoundedBitmapDisplayer(20))
                    // .displayer(new FadeInBitmapDisplayer(50))// 淡入
            .build();

    public static DisplayImageOptions bgImageOptions = new DisplayImageOptions.Builder()
            // .showImageForEmptyUri(R.drawable.img_user_info_bg)
            // .showImageOnLoading(R.drawable.img_user_info_bg)
            // .showImageOnFail(R.drawable.img_user_info_bg)
            .cacheInMemory(true).cacheOnDisk(true).considerExifParams(true)
            .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
            .bitmapConfig(Config.RGB_565).resetViewBeforeLoading(true)// 设置图片在下载前是否重置，复位
                    // .displayer(new RoundedBitmapDisplayer(20))
                    // .displayer(new FadeInBitmapDisplayer(50))// 淡入
            .build();

    public static DisplayImageOptions bigImageOptions = new DisplayImageOptions.Builder()
            .showImageForEmptyUri(R.drawable.empty_photo)
                    // .showImageOnFail(R.drawable.image_load_fail)
            .cacheInMemory(true).cacheOnDisk(true).considerExifParams(true)
            .imageScaleType(ImageScaleType.EXACTLY)
            .bitmapConfig(Config.RGB_565).resetViewBeforeLoading(true)// 设置图片在下载前是否重置，复位
                    // .displayer(new RoundedBitmapDisplayer(20))
                    // .displayer(new FadeInBitmapDisplayer(50))// 淡入
            .build();

    public static DisplayImageOptions splashImageOptions = new DisplayImageOptions.Builder()
            // .showImageForEmptyUri(R.drawable.welcome1)
            // .showImageOnFail(R.drawable.image_load_fail)
            .cacheOnDisk(true).considerExifParams(true)
            .imageScaleType(ImageScaleType.EXACTLY)
            .bitmapConfig(Config.RGB_565).resetViewBeforeLoading(true)//
                    // .displayer(new RoundedBitmapDisplayer(20))
                    // .displayer(new FadeInBitmapDisplayer(50))// 淡入
            .build();

    public static DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
            .cacheInMemory(true).cacheOnDisk(true)
            .displayer(new FadeInBitmapDisplayer(600))
                    // .showImageOnLoading(R.drawable.ic_default_pic)
            .build();

    static int memClass = ((ActivityManager) MLApplication.getInstance()
            .getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
    static int cacheSize = 1024 * 1024 * memClass / 8; // 1/8 of system ram

    public static ImageLoaderConfiguration getImageLoaderConfig(
            Context context, File cacheDir) {
        return new ImageLoaderConfiguration.Builder(context)
                .threadPoolSize(2)
                .denyCacheImageMultipleSizesInMemory()
                .memoryCache(new LruMemoryCache(cacheSize))
                .memoryCacheSize(cacheSize)
                .diskCache(new UnlimitedDiskCache(cacheDir))
                        // 自定义缓存路径
                        // default
                .diskCacheSize(50 * 1024 * 1024).diskCacheFileCount(100)
                .defaultDisplayImageOptions(defaultOptions).build();

    }
}
